;var module = angular.module('CtrlsContent');
module.controller('ContentCtrl',['$scope','$filter','$state','api_other','$timeout','$stateParams',function($scope,$filter,$state,api_other,$timeout,$stateParams){
	$scope.href = "zhangjieList";
	$scope.contentModel = {
		model:{},
		get:function(page){
			var self = this;
			var obj = {
				catId:localStorage.zhangjieCatId,
				page:page
			};
			/*if(!localStorage.zhangjieCatId){
				alert('请选择章节列表');
				$state.go('main');
				return;
			}*/
			var promise = api_other.testContent(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model.options = [];
					self.model = data.returnData[0];
				}else{
					localStorage.page = 1;
					alert('后面没题了,请重新选择列表');
					$state.go('main');
				}
			});
			return promise;
		},
		wrongGet:function(userId,page){
			var self = this;
			var obj = {
				userId:localStorage.bls_userId,
				page:page
			};
			var promise = api_other.shoucangList(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model.options = [];
					self.model = data.returnData[0];
					self.model.options = JSON.parse(self.model.options);
				}else{
					localStorage.collectPage = 1;
					alert('后面没题了,即将返回');
					$state.go('main');
				}
			});
			return promise;
		},
		collectGet:function(userId,page){
			var self = this;
			var obj = {
				userId:localStorage.bls_userId,
				page:page
			};
			/*if(!localStorage.zhangjieCatId){
				alert('请选择章节列表');
				$state.go('main');
				return;
			}*/
			var promise = api_other.shoucangList(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.model.options = [];
					self.model = data.returnData[0];
					self.model.options = JSON.parse(self.model.options);
				}else{
					localStorage.wrongPage = 1;
					alert('后面没题了,即将返回');
					$state.go('main');
				}
			});
			return promise;
		}
	};
	$scope.statusFlag = {
		page:1,
		answerShow:false,
		resolveShow:false,
		moreSelect:[],
		
		singleSelect:"",
		collect:false,
		beisong:false
	};
	$scope.event = {
		init:function(){
			if($stateParams.param == 3){
				$scope.href = "main";
				var page = localStorage.wrongPage || 1;
				$scope.statusFlag.page = page;
				$scope.contentModel.wrongGet(1,page);					
			}else if($stateParams.param == 1){
				$scope.href = "zhangjieList";
				var page = localStorage.page || 1;
				$scope.statusFlag.page = page;
				$scope.contentModel.get(page);
			}else if($stateParams.param == 4){
				$scope.href = "main";
				var page = localStorage.collectPage || 1;
				$scope.statusFlag.page = page;
				$scope.contentModel.collectGet(1,page);
			}
		},
		preQuestion:function(){
			$scope.statusFlag.page--;
			$scope.statusFlag.answerShow = false;
			$scope.statusFlag.resolveShow = false;
			$scope.statusFlag.beisong = false;
			if($scope.statusFlag.page <=1){
				$scope.statusFlag.page =1;
			}
			if($stateParams.param == 3){
				localStorage.wrongPage = $scope.statusFlag.page;
				$scope.contentModel.wrongGet(1,$scope.statusFlag.page);
			}else if($stateParams.param == 1){
				localStorage.page = $scope.statusFlag.page;
				$scope.contentModel.get($scope.statusFlag.page);
			}else if($stateParams.param == 4){
				localStorage.collectPage = $scope.statusFlag.page;
				$scope.contentModel.collectGet(1,$scope.statusFlag.page);
			}
			
		},
		nextQuestion:function(){
			$scope.statusFlag.page++;
			$scope.statusFlag.answerShow = false;
			$scope.statusFlag.resolveShow = false;
			$scope.statusFlag.beisong = false;
			if($stateParams.param == 1){
				localStorage.page = $scope.statusFlag.page;
				$scope.contentModel.get($scope.statusFlag.page);
			}else if($stateParams.param == 3){
				localStorage.wrongPage = $scope.statusFlag.page;
				$scope.contentModel.wrongGet(1,$scope.statusFlag.page);
			}else if($stateParams.param == 4){
				localStorage.collectPage = $scope.statusFlag.page;
				$scope.contentModel.collectGet(1,$scope.statusFlag.page);
			}
		},
		moreSelect:function(key){
			if($scope.statusFlag.answerShow)
				return ;
			if($scope.statusFlag.moreSelect.toString().indexOf(key) ==-1){
				$scope.statusFlag.moreSelect.push(key);
				$scope.statusFlag.moreSelect.sort();
				//$filter('orderBy')($scope.statusFlag.moreSelect, '+');
			}else{
				$scope.statusFlag.moreSelect.splice($scope.statusFlag.moreSelect.toString().replace(/,/g,'').indexOf(key),1);
			}
		},
		moreSelectResolve:function(){
			if($scope.statusFlag.moreSelect.length==0){
				alert('请选择答案');
				return ;
			}
			$scope.statusFlag.answerShow=true;
			var flag = this.isTrue();
			if(flag){
			}else{
				var promise = api_other.wrongSubmit({
					questionId:$scope.contentModel.model.id,
					userId:localStorage.bls_userId,
					answer:$scope.statusFlag.moreSelect.toString().replace(/,/g,''),
				});				
			}
		},
		singleSelect:function(key){
			if($scope.statusFlag.answerShow)
				return ;
			$scope.statusFlag.singleSelect = key;
			var flag = this.isTrue();
			if(flag){
			}else{
				var promise = api_other.wrongSubmit({
					questionId:$scope.contentModel.model.id,
					userId:localStorage.bls_userId,
					answer:$scope.statusFlag.singleSelect,
				});
			}
			$timeout(function(){
				$scope.statusFlag.answerShow=true; 
			},10);
		},
		isTrue:function(){
			if($scope.contentModel.model.type == "2"){
				if($scope.contentModel.model.answer == $scope.statusFlag.moreSelect.toString().replace(/,/g,'')){
					return true
				}else{
					return false
				}
			}else{
				if($scope.contentModel.model.answer == $scope.statusFlag.singleSelect){
					return true
				}else{
					return false
				}
			}
		},
		collect:function(){ 
			var obj = {
				userId:localStorage.bls_userId,
				questionId:$scope.contentModel.model.id
			};
			var promise = api_other.collectTimu(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					$scope.statusFlag.collect = true;
					$timeout(function(){
						$scope.statusFlag.collect = false;
					},2000);
				}
			});
		},

	};

	$scope.event.init();
}])